<template>
  <div class="channel-edit-page">
    <!-- 我的频道 -->
    <van-cell class="my-channel">
      <div class="title" slot="title">我的频道</div>
      <van-button
        class="editBtn"
        slot="default"
        size="mini"
        round
        @click="showClearBtn=!showClearBtn"
      >{{!this.showClearBtn?'编辑':'完成'}}</van-button>
    </van-cell>
    <!-- 宫格 -->
    <van-grid :column-num="4" :gutter="10">
      <!-- 推荐频道不能删 -->
      <van-grid-item
        class="channel-item"
        v-for="(item,index) in channelList"
        :key="index"
        @click="handelChannelClick(item,index)"
      >
        <!-- 删除频道小按钮 -->
        <van-icon
          class="icon-clear"
          slot="icon"
          name="clear"
          v-show="showClearBtn&&!notDelChannelsID.includes(item.id)"
        ></van-icon>
        <span
          :class="{'channel-name':true,'active-color':active==item.id}"
          slot="text"
        >{{item.name}}</span>
      </van-grid-item>
    </van-grid>

    <!-- 频道编辑 -->
    <van-cell class="edit-channel">
      <div class="title" slot="title">频道编辑</div>
    </van-cell>
    <van-grid :column-num="4" :gutter="10">
      <van-grid-item
        class="channel-item"
        v-for="(item,index) in recChannelsList"
        :key="index"
        @click="addChannels(item)"
      >
        <van-icon class="icon-plus" slot="icon" name="plus"></van-icon>
        <span class="channel-name" slot="text">{{item.name}}</span>
      </van-grid-item>
    </van-grid>
  </div>
</template>

<script>
import { getChannelList, addChannel, delChannel } from '../../api/article'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage'
export default {
  props: ['channelList', 'active'],
  data() {
    return {
      allChannels: [],
      // clear按钮控制
      showClearBtn: false,
      // 默认的不要删除的频道id
      notDelChannelsID: [0]
    }
  },
  created() {
    this.loadChannelsList()
  },
  methods: {
    async loadChannelsList() {
      // 获取所有频道

      try {
        const { data: res } = await getChannelList()
        console.log(res)
        this.allChannels = res.data.channels
      } catch (err) {
        console.log(err)
        this.$toast.fail('获取所有频道失败')
      }
    },
    async addChannels(item) {
      // 添加频道到我的
      // this.channelList.push(item) 不建议直接修改父组件数据
      this.$emit('changeChannels', item)
      // 未登录保存在本地
      if (!this.userToken) {
        setItem('unlogin-channels', this.channelList)
      } else {
        try {
          await addChannel([
            {
              id: item.id, // 频道id
              seq: this.channelList.length // 频道的序号
            }
          ])
        } catch (err) {
          console.log(err)
          this.$toast.fail('添加频道失败')
        }
      }
    },
    async handelChannelClick(item, index) {
      // 当处于编辑状态时，点击时删除频道，激活的索引要变化
      if (this.showClearBtn) {
        // 推荐不能删
        if (this.notDelChannelsID.includes(item.id)) return
        this.channelList.splice(index, 1)
        if (!this.userToken) {
          // 未登录存储在本地
          setItem('unlogin-channels', this.channelList)
        } else {
          try {
            await delChannel(item.id)
          } catch (err) {
            console.log(err)
            this.$toast.fail('删除频道失败')
          }
        }
      }
      // 当处于完成状态时，点击是切换频道，通过子传父改变active值
      if (!this.showClearBtn) {
        this.$emit('changeActive', item.id)
      }
    }
  },
  computed: {
    ...mapState(['userToken']),
    recChannelsList() {
      return this.allChannels.filter(item => {
        return !this.channelList.find(i => i.id === item.id)
      })
    }
  }
}
</script>

<style lang='less' scoped>
.channel-edit-page {
  .active-color {
    color: rgb(65, 160, 70) !important;
    font-weight: 600;
  }
  .my-channel {
    position: relative;
    margin-top: 90px;
    padding-bottom: 30px;
    &::after {
      content: none;
    }
  }
  .edit-channel {
    padding-top: 30px;
    &::after {
      content: none;
    }
  }
  .title {
    font-size: 30px;
  }
  .editBtn {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }
  /deep/ .channel-item {
    margin: 10px 0 20px 0;
    position: relative;
    .van-grid-item__icon-wrapper {
      position: unset;
      i.icon-clear {
        position: absolute !important;
        font-size: 30px;
        color: #cacaca;
        top: -10px;
        right: -10px;
        z-index: 4;
      }
    }

    /deep/ .van-grid-item__content {
      flex-direction: row;
      background-color: #f4f5f6;
      width: 160px;
      height: 86px;
      border-radius: 10%;
      .icon-plus {
        font-size: 28px;
        margin: 5px 6px 0 0;
      }
      .channel-name {
        white-space: nowrap;
        font-size: 28px;
        color: #222;
      }
    }
  }
}
</style>
